﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.Material;
using PloobsEngine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using PloobsEngine.Modelo;

namespace Etapa1.BezierExperiments
{
    public class IndexVertexTriangleShader : IShader
    {
        public IndexVertexTriangleShader(String shaderName , FillMode fillMode)
        {
            this.shaderName = shaderName;
            this.fillMode = fillMode;
        }

        public IndexVertexTriangleShader()
        {
        }

        public override MaterialType MaterialType
        {
            get { return MaterialType.DEFERRED; }
        }

        FillMode fillMode = FillMode.Solid;
        String shaderName = null;
        Effect _shader;
        public override void Initialize()
        {
            if (String.IsNullOrEmpty(shaderName))
            {
                _shader = EngineStuff.CustomContentManager.GetAsset<Effect>("Effects/bezier");
            }
            else
            {
                _shader = EngineStuff.CustomContentManager.GetAsset<Effect>(shaderName);
            }
            base.Initialize();
        }

        public override void Draw(IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam)
        {
            PosDrawPhase(modelo, render, cam, lights);
        }

        public override void  PosDrawPhase(IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam, IList<PloobsEngine.Light.ILight> lights)
        {

            VertexIndexBufferModel vbm = modelo as VertexIndexBufferModel;

            _shader.Parameters["World"].SetValue(obj.getWorldMatrix());
            _shader.Parameters["View"].SetValue(cam.View);
            _shader.Parameters["Projection"].SetValue(cam.Projection);
            BatchInformation bi = vbm.GetBatchInformation(0)[0];
                       

            this._shader.Begin();
            EngineStuff.GraphicsDevice.VertexDeclaration = bi.VertexDeclaration;
            EngineStuff.GraphicsDevice.Vertices[0].SetSource(vbm.GetVertexBuffer(0), 0, bi.VertexDeclaration.GetVertexStrideSize(0));
            EngineStuff.GraphicsDevice.Indices = vbm.GetIndexBuffer(0);

            EngineStuff.GraphicsDevice.RenderState.FillMode = fillMode;
            EngineStuff.GraphicsDevice.RenderState.CullMode = CullMode.None;

            this._shader.CurrentTechnique.Passes[0].Begin();
            EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0 ,bi.NumVertices,0, bi.PrimitiveCount);
            this._shader.CurrentTechnique.Passes[0].End();
            this._shader.End();

            EngineStuff.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
            EngineStuff.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
        }
    }
}
